//package com.nwpu.server.gateway.filter;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.auth0.jwt.interfaces.Claim;
//import com.nwpu.server.common.response.R;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.context.annotation.Bean;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.utils.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
///**
// * @Author: YuXingZh
// * @Date: Created in 22:27 2022-05-20
// * @Description:
// */
//@Component
//public class TokenFilter implements GlobalFilter, Ordered {
//    /**
//     * 校验token
//     *
//     * @param exchange
//     * @param chain
//     * @return
//     */
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        String token = request.getHeaders().getFirst(RequestKeyConstants.TOKEN);
//        //检查token是否为空
//        if (StringUtils.isEmpty(token)) {
//            return denyAccess(exchange, ResultCode.TOKEN_NULL);
//        }
//
//        Map claimMap1 = jwtUtils.parseJwt(token);
//        //token有误
//        if (claimMap.containsKey("exception")) {
//            log.error() (claimMap1.get("exception").toString());
//            return denyAccess(exchange, ResultCode.TOKEN_INVALID);
//        }
//
//        //token无误，将用户信息设置进header中,传递到下游服务
//        Map<String, Claim> claimMap = claimMap1;
//        String userId = claimMap.get(RequestKeyConstants.USER_ID).asString();
//        Consumer<HttpHeaders> headers = httpHeaders -> {
//            httpHeaders.add(RequestKeyConstants.USER_ID, userId);
//        };
//        request.mutate().headers(headers).build();
//
//        // todo 权限校验
//
//        return chain.filter(exchange);
//    }
//
//    /**
//     * 拦截并返回自定义的json字符串
//     */
//    private Mono<Void> denyAccess(ServerWebExchange exchange, R resultCode) {
//        ServerHttpResponse response = exchange.getResponse();
//        response.setStatusCode(HttpStatus.OK);
//        //这里在返回头添加编码，否则中文会乱码
//        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//        byte[] bytes = JSON.toJSONBytes(R.error(resultCode), SerializerFeature.WriteMapNullValue);
//        DataBuffer buffer = response.bufferFactory().wrap(bytes);
//        return response.writeWith(Mono.just(buffer));
//    }
//
//    @Override
//    public int getOrder() {
//        return -1;
//    }
//
//}